<?php

defined('IN_XLP') or exit('Access Denied');

/*
 * 周边商家
 */
class aroundAction extends commonAction {

	function __construct() {
		parent::__construct();
		parent::_checkLogin();
	}

	public function index() {

		$page = is_null($this->_get('page')) ? 1 : $this->_get('page');
		$searchtype = '';
		$keyword	= '';
		$where	  = ' 1=1 ';
		
		if($_GET){
			$searchtype = $this->_get('searchtype') ? $this->_get('searchtype') : '';
			$keyword	= $this->_get('keyword') ? $this->_get('keyword') : '';
			
			switch ($searchtype) {
				case '1':
					$where .= " and `shopName` like '%".$keyword."%'";
					break;
				case '2':
					$where .= " and `phoneNo` like '%".$keyword."%'";
					break;
				case '3':
					$where .= " and `address` like '%".$keyword."%'";
					break;
				default:
					# code...
					break;
			}
		}

		$list  = M('business')->page($page, 20)->where($where)->order('`id`')->findAll();
		$total = M('business')->getTotal();


		$this->assign('searchtype', $searchtype);
		$this->assign('keyword', $keyword);
		$this->assign('total', $total);
		$this->assign('list', $list);
		$this->assign('page', $page);

		$this->display();
	}


	public function add() {

		$tablefields = M('business')->getTableFields();
		$data = array();

		foreach ($tablefields['fields'] as $key => $value) {
			$data[$key] = $this->_post($key) ? $this->_post($key) : null;
			if($data[$key] == null){
				unset($data[$key]);
			}
		}

		$result = M('business')->insert($data);
		if($result) {
			$this->JsonReturn('添加成功', null, 1);
		} else {
			$this->JsonReturn('添加失败', null, 0);
		}
				
	}


	public function update() {

		$id = $this->_post('shop_id');
		if($id) {
			$col = $this->_post('property');
			$val = $this->_post('value');

			$result = M('business')->update(array("$col"=>$val), array('id'=>$id));
			if($result) 
				$this->JsonReturn('编辑成功', null, 1);
		}
		$this->JsonReturn('编辑失败', null, 0);
	}


	public function delete() {
		$id = $this->_post('shop_id');
		if($id) {
			$result = M('business')->delete(array('id'=>$id));
			if($result) 
				$this->JsonReturn('删除成功', null, 1);	
		}
		$this->JsonReturn('删除失败', null, 0);
	}


	/*
	 * 优化坐标
	 */
	public function improve() {
		$this->display();
	}


	/*
	 * 优化坐标用
	 */
	public function map() {
		$id = $this->_post('shop_id');
		$result = M('business')->field('`geoLat`,`geoLng`')->where('`id`='.$id)->find();

		if(!$result) sendJson(array('err'=>0,'stop'=>0));

		if($id=='47884') sendJson(array('err'=>0,'stop'=>1));

		$y = $result['geoLat'];
		$x = $result['geoLng'];

		$url  = 'http://apis.map.qq.com/ws/coord/v1/translate?locations=' . $y . ',' . $x . '&type=3&key=1aa6a77c877c9d026f2f7640bb722f41';
		$cont = getHttp($url);
		$map  = array('lat'=>0,'lng'=>0);
		if ($cont) {
			$contArr = json_decode($cont, true);
			if (!$contArr['status']) {
				$map['lat'] = isset($contArr['locations'][0]['lat']) ? $contArr['locations'][0]['lat'] : 0;
				$map['lng'] = isset($contArr['locations'][0]['lng']) ? $contArr['locations'][0]['lng'] : 0;
			}
		}


		$result = D('business')
				->update(array('newLat'=>"{$map['lat']}",'newLng'=>"{$map['lng']}"), array('id'=>$id));

		if($result) {
			sendJson(array('err'=>0,'stop'=>0,'msg'=>array($map['lat'],$map['lng'])));
		} else {
			sendJson(array('err'=>1,'stop'=>0));
		}
	}
}